<?php
class Mission_logAction extends IntesysAction {
	//初始化
	public function _initialize(){
		parent::_initialize();
		C('TOKEN_ON', false);
		$_POST['site_id'] = __SITEID__;
		$this->dao = D('Mission');
	}
	//显示可选择表格，用户从这里选择一个电子表格
	public function index(){
		//直接上级或者审批人查看
		$this->pagesize = 8;
		$this->listmap = "state >= 0 and site_id = ". __SITEID__." and (approver_id = '".$this -> memberInfo['id']."' or approver like '%,".$this -> memberInfo['id'].",%')";
		
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and name like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		
		parent::_list();
		$this->display();
	}
	
	
	//我填写的审批
	public function mymission(){
		//直接上级或者审批人查看
		$this->pagesize = 8;
		$this->listmap = "site_id = ". __SITEID__." and (member_id = '".$this -> memberInfo['id']."')";
		
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and name like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		
		parent::_list();
		$this->display();
	}
	
	
	//查看审批
	public function mymissionread(){
		$this->initView();
		$id = $this->_get('id', 'intval');
		//查找一条记录
		$map['id'] = $id;
		$map['site_id'] = __SITEID__;
		$info = $this -> dao -> where($map) -> find();
		//表单数据
		$formdata = unserialize($info['object']);
		//表单模板
		$formtemplate = $info['formtemplate'];
		//还原表单
		$this->html = $this->fetch('','<div class="hidden"></div>'.$formtemplate);
		$db = M('Schedule');
		$schedulemap['mission_id'] = $id;
		$schedulemap['member_id'] = $this->memberInfo['id'];
		$this->schedule = $db -> where($schedulemap) -> find();
		$this->assign('info', $info);
		$this->display();
	}
	
	

	
	
	//查看审批
	public function read(){
		$this->initView();
		$id = $this->_get('id', 'intval');
		//查找一条记录
		$map['id'] = $id;
		$map['site_id'] = __SITEID__;
		$info = $this -> dao -> where($map) -> find();
		//表单数据
		$formdata = unserialize($info['object']);
		//表单模板
		$formtemplate = $info['formtemplate'];
		//还原表单,加个空DIV防止模板代码为空时耗光内存
		$this->html = $this->fetch('','<div class="hidden"></div>'.$formtemplate);
		$db = M('Schedule');
		$schedulemap['mission_id'] = $id;
		$schedulemap['member_id'] = $this->memberInfo['id'];
		$this->schedule = $db -> where($schedulemap) -> find();
		$this->assign('info', $info);
		$this->display();
	}
	
	
	//递交表格
	public function send(){
		if($this->_post('id')){
			$mission = $this -> dao -> where(array('id' => $this->_post('id'))) -> find();
			if($mission['state'] >= 0){
				$this->error('该审批已经递交，请不要重复操作');
			}else{
				$ret = $this -> dao -> where(array('id' => $this->_post('id'))) -> save(array('state' => 1));
				if($ret){
					$this->success('递交成功');
				}else{
					$this->error($this->dao -> getError());
				}
			}
		}else{
			$this->_post('参数错误!');
		}
		
	}
	
	//取消申请
	public function cancel(){
		if($this->_post('id')){
			$mission = $this -> dao -> where(array('id' => $this->_post('id'))) -> find();
			if($mission['state'] == 3){
				$this->error('已经通过的审批不能取消！');
			}else{
				$ret = $this -> dao -> where(array('id' => $this->_post('id'))) -> save(array('state' => -1));
				if($ret){
					$this->success('取消成功');
				}else{
					$this->error($this->dao -> getError());
				}
			}
		}else{
			$this->_post('参数错误!');
		}
		
	}
	
	
	//通过审批
	public function pass(){
		if(IS_POST){
			$db = M('Schedule');
			$mission = $this -> dao -> where(array('id' => $this->_post('mission_id'))) -> find();
			//找一条记录出来
			//$map['id'] = $this->_post('id');
			$map['mission_id'] = $this->_post('mission_id');
			$map['member_id'] = $this->memberInfo['id'];
			$info = $db -> where($map) -> find();
			$_POST['member_id'] = $this->memberInfo['id'];
			$_POST['datatime'] = time();
			if($info){
				$ret = $db -> where($map) -> data($_POST) -> save();
			}else{
				$ret = $db -> data($_POST) -> add();
			}
			if($ret){
				//是否递交上级
				if(isset($_POST['send'])){
					$this -> dao -> where(array('id' => $map['mission_id'])) -> data(array('approver' => str_replace($this->memberInfo['pid'].',', '',$mission['approver']).$this->memberInfo['pid'].',')) -> save();
				}
				
				//更新当前审批状态
				$mission = $this -> dao -> where(array('id' => $this->_post('mission_id'))) -> find();
				$approvercount = $mission['approver_id'] > 0 ? count(explode(',', $mission['approver'])) - 1 : count(explode(',', $mission['approver'])) - 2;
				$schedule = $db -> where(array('member_id' => array('in', trim($mission['approver'], ',').','.$mission['approver_id']), 'mission_id' => $mission['id'])) -> select();dump($db -> getLastSql());
				if(count($schedule) == 0){
					//没有审批记录
					$data['state'] = 1;
				}else{
					$passcount = $turnedcount = 0;
					foreach($schedule as $k => $v){
					    if($v['status'] == 1){
							$passcount ++;
						}else{
							$turnedcount ++;
						}
				    }
					if($passcount == $approvercount){
						//通过
						$data['state'] = 3;
					}
					if($passcount < $approvercount && $turnedcount == 0){
						//审批中
						$data['state'] = 2;
					}
					if($turnedcount > 0){
						//驳回
						$data['state'] = 0;
					}
				}
				
				//更新状态 
				$ret = $this -> dao -> data($data) -> where(array('id' => $this->_post('mission_id'))) -> save();
				if($ret !== false && $data['state'] == 3){
					//通过审批,更新状态成功，在这里这样计算分数
					$this->initView();
					$this->dao -> where(array('id' => $mission['id'])) -> createevent($this-> view);
				}
				
				$this->success('操作成功');
			}else{
				$this->error($db -> getError());
			}
			$this->success();
		}else{
			$this->error('参数错误');
		}
		
	}
	
}